package com.zzx.filter;

import com.zzx.Util.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: Spider Man
 * @Description:
 * @Date Created in 2020-09-15 10:38
 * @Modified By:
 */
@WebFilter({"/admin/index.html","/admin/view/*","/express/*","/courier/*","/user/*"})
public class AccessControlFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String username = UserUtil.createUsername(request.getSession());
        if (username != null){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            response.sendError(404,"您还未登录");
        }
    }

    @Override
    public void destroy() {

    }
}
